﻿namespace AddressBook.Web
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;

  using Business.Contact;
	using Business.Event;
	using Business.Person;
	#endregion

	public partial class ManageLocations : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
			if (!this.Page.IsPostBack)
			{
				this.BindLocationList();
				this.BindAddressData();
			}
    }

		#region "Bind Functions"

		private void BindLocationList()
		{
			LocationList locations = LocationList.GetLocationList();
			this.ManageLocationsLocationList.DataSource = locations;
			this.ManageLocationsLocationList.DataBind();

      this.LocationErrorsPanel.Visible = false;
		}

		private void BindAddressData()
		{
			StateProvinceList states = StateProvinceList.GetStateProvinceList();

			this.AddLocationStateDropDown.DataSource = states;
			this.AddLocationStateDropDown.DataValueField = "Id";
			this.AddLocationStateDropDown.DataTextField = "StateProvinceCode";
			this.AddLocationStateDropDown.DataBind();

			this.EditLocationStateDropDown.DataSource = states;
			this.EditLocationStateDropDown.DataValueField = "Id";
			this.EditLocationStateDropDown.DataTextField = "StateProvinceCode";
			this.EditLocationStateDropDown.DataBind();
		}

		#endregion

		#region "Add Location Events"

		protected void AddLocationSaveButton_Click(object sender, ImageClickEventArgs e)
		{
			Location location = Location.NewLocation();

			location.Name = this.AddLocationNameTextBox.Text;
			location.Address.Address1 = this.AddLocationAddressLine1TextBox.Text;
			location.Address.Address2 = this.AddLocationAddressLine2TextBox.Text;
			location.Address.City = this.AddLocationCityTextBox.Text;
			location.Address.StateProvinceId = int.Parse(this.AddLocationStateDropDown.SelectedValue);
			location.Address.PostalCode = this.AddLocationPostalCodeTextBox.Text;

			location = location.Save();
			this.BindLocationList();

			this.AddLocationNameTextBox.Text = string.Empty;
			this.AddLocationAddressLine1TextBox.Text = string.Empty;
			this.AddLocationAddressLine2TextBox.Text = string.Empty;
			this.AddLocationCityTextBox.Text = string.Empty;
			this.AddLocationStateDropDown.SelectedIndex = -1;
			this.AddLocationPostalCodeTextBox.Text = string.Empty;
		}

		#endregion

		#region "Location List Events"

		protected void ManageLocationsLocationList_ItemCommand(object sender, ListViewCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "Edit":
					Location location = Location.GetLocation(int.Parse(e.CommandArgument.ToString()));

					this.EditLocationIdField.Value = location.Id.ToString();
					this.EditLocationNameTextBox.Text = location.Name;
					this.EditLocationAddress1TextBox.Text = location.Address.Address1;
					this.EditLocationAddress2TextBox.Text = location.Address.Address2;
					this.EditLocationCityTextBox.Text = location.Address.City;
					this.EditLocationStateDropDown.SelectedValue = location.Address.StateProvinceId.ToString();
					this.EditLocationPostalCodeTextBox.Text = location.Address.PostalCode;

					this.EditLocationPanelPopupExtender.Show();
					break;

				case "Delete":
          EventList events = EventList.GetEventList();

          if (events.Count(ev => ev.LocationId == int.Parse(e.CommandArgument.ToString())) == 0)
          {
            Location.DeleteLocation(int.Parse(e.CommandArgument.ToString()));
            this.BindLocationList();
          }
          else
          {
            this.LocationErrorsPanel.Visible = true;
            this.LocationErrorMessage.Text = "You cannot delete this location while it is assigned to an event.";
          }

					break;
			}
		}

		protected void ManageLocationsLocationList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
		{

		}

		protected void ManageLocationsLocationList_ItemEditing(object sender, ListViewEditEventArgs e)
		{

		}

		protected void EditLocationSaveButton_Click(object sender, ImageClickEventArgs e)
		{
			Location location = Location.GetLocation(int.Parse(this.EditLocationIdField.Value));

			location.Name = this.EditLocationNameTextBox.Text;
			location.Address.Address1 = this.EditLocationAddress1TextBox.Text;
			location.Address.Address2 = this.EditLocationAddress2TextBox.Text;
			location.Address.City = this.EditLocationCityTextBox.Text;
			location.Address.StateProvinceId = int.Parse(this.EditLocationStateDropDown.SelectedValue);
			location.Address.PostalCode = this.EditLocationPostalCodeTextBox.Text;

			location = location.Save();
			this.BindLocationList();

			this.EditLocationNameTextBox.Text = String.Empty;
			this.EditLocationAddress1TextBox.Text = String.Empty;
			this.EditLocationAddress2TextBox.Text = String.Empty;
			this.EditLocationCityTextBox.Text = String.Empty;
			this.EditLocationStateDropDown.SelectedIndex = -1;
			this.EditLocationPostalCodeTextBox.Text = String.Empty;
		}

		#endregion
	}
}
